package org.jeecg.modules.demo.cms.article.controller;

import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.query.QueryRuleEnum;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.cms.article.entity.CmsArticle;
import org.jeecg.modules.demo.cms.article.service.ICmsArticleService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.demo.cms.article.vo.CmsArticleVo;
import org.jeecg.modules.demo.cms.article_content.service.ICmsArticleContentService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 网站新闻表
 * @Author: jeecg-boot
 * @Date:   2025-06-07
 * @Version: V1.0
 */
@Tag(name="网站新闻表")
@RestController
@RequestMapping("/cms/article/cmsArticle")
@Slf4j
public class CmsArticleController extends JeecgController<CmsArticle, ICmsArticleService> {
	@Autowired
	private ICmsArticleService cmsArticleService;
	@Autowired
	private ICmsArticleContentService cmsArticleContentService;

	/**
	 * 分页列表查询
	 *
	 * @param cmsArticle
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "网站新闻表-分页列表查询")
	@Operation(summary="网站新闻表-分页列表查询")
	@GetMapping(value = "/list1")
	public Result<IPage<CmsArticle>> queryPageList(CmsArticle cmsArticle,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
        QueryWrapper<CmsArticle> queryWrapper = QueryGenerator.initQueryWrapper(cmsArticle, req.getParameterMap());
		Page<CmsArticle> page = new Page<CmsArticle>(pageNo, pageSize);
		IPage<CmsArticle> pageList = cmsArticleService.page(page, queryWrapper);
		return Result.OK(pageList);
	}


	 /**
	  * 分页列表查询 返回 内容属性
	  *
	  * @param cmsArticle
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 //@AutoLog(value = "网站新闻表-分页列表查询")
	 @Operation(summary="网站新闻表-分页列表查询")
	 @GetMapping(value = "/list")
	 public Result<IPage<CmsArticleVo>> queryPageList1(CmsArticle cmsArticle,
													   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
													   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
													   HttpServletRequest req) {
		 QueryWrapper<CmsArticle> queryWrapper = QueryGenerator.initQueryWrapper(cmsArticle, req.getParameterMap());

		 Page<CmsArticleVo> page = new Page<CmsArticleVo>(pageNo, pageSize);
		 IPage<CmsArticleVo> pageList = cmsArticleService.pageSelectArticle(page,queryWrapper);
		 return Result.OK(pageList);
	 }
	
	/**
	 *   添加
	 *
	 * @param cmsArticleVo
	 * @return
	 */
	@AutoLog(value = "网站新闻表-添加")
	@Operation(summary="网站新闻表-添加")
	@RequiresPermissions("cms.article:cms_article:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody CmsArticleVo cmsArticleVo,HttpServletRequest request) {
		cmsArticleVo.setZhuangt("1");
//		String  username = JwtUtil.getUserNameByToken(request);
//		cmsArticleVo.setCreateuser(username);
//		cmsArticleVo.setCreatetime(new Date());
		cmsArticleService.saveCmsArticleVo(cmsArticleVo);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param cmsArticle
	 * @return
	 */
	@AutoLog(value = "网站新闻表-编辑")
	@Operation(summary="网站新闻表-编辑")
	@RequiresPermissions("cms.article:cms_article:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody CmsArticleVo cmsArticleVo, HttpServletRequest request) {

		cmsArticleVo.setZhuangt("1");
		cmsArticleService.updateById(cmsArticleVo);

		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "网站新闻表-通过id删除")
	@Operation(summary="网站新闻表-通过id删除")
	@RequiresPermissions("cms.article:cms_article:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
//		cmsArticleService.removeById(id);
		cmsArticleService.deleteArticleById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "网站新闻表-批量删除")
	@Operation(summary="网站新闻表-批量删除")
	@RequiresPermissions("cms.article:cms_article:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.cmsArticleService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "网站新闻表-通过id查询")
	@Operation(summary="网站新闻表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<CmsArticle> queryById(@RequestParam(name="id",required=true) String id) {
		CmsArticle cmsArticle = cmsArticleService.getById(id);
		if(cmsArticle==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(cmsArticle);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param cmsArticle
    */
    @RequiresPermissions("cms.article:cms_article:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CmsArticle cmsArticle) {
        return super.exportXls(request, cmsArticle, CmsArticle.class, "网站新闻表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("cms.article:cms_article:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CmsArticle.class);
    }


	 /**
	  * 自定义方法
	  */

//	 submitShenhe
	 /**
	  *  审核
	  *
	  * @param cmsArticle
	  * @return
	  */
	 @AutoLog(value = "网站新闻表-提交到审核")
	 @Operation(summary="网站新闻表-提交到审核")
	 @RequiresPermissions("cms.article:cms_article:submitShenhe")
	 @RequestMapping(value = "/submitShenhe", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<String> submitShenhe(@RequestBody CmsArticle cmsArticle, HttpServletRequest request) {

		 String  username = JwtUtil.getUserNameByToken(request);

		 CmsArticle cmsArticle1 = new CmsArticle();
		 cmsArticle1.setId(cmsArticle.getId());
		 cmsArticle1.setZhuangt("2");
//		 cmsArticle1.setShenhr(username);
//		 cmsArticle1.setShenhshj(new Date());
//		 cmsArticle1.setShenheyuany(cmsArticle.getShenheyuany());
//		cmsArticleService.updateById(cmsArticle);
		 cmsArticleService.updateById(cmsArticle1);
//		cmsArticleContentService.saveOrUpdate(cmsArticleContent);

		 return Result.OK("审核成功!");
	 }

	 /**
	  *  审核
	  *
	  * @param cmsArticle
	  * @return
	  */
	 @AutoLog(value = "网站新闻表-审核")
	 @Operation(summary="网站新闻表-审核")
	 @RequiresPermissions("cms.article:cms_article:shenhe")
	 @RequestMapping(value = "/shenhe", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<String> shenhe(@RequestBody CmsArticle cmsArticle, HttpServletRequest request) {

		 String  username = JwtUtil.getUserNameByToken(request);

		 CmsArticle cmsArticle1 = new CmsArticle();
		 cmsArticle1.setId(cmsArticle.getId());
		 cmsArticle1.setZhuangt("3");
		 cmsArticle1.setShenheyuany("审核通过");
		 cmsArticle1.setShenhr(username);
		 cmsArticle1.setShenhshj(new Date());
		 cmsArticle1.setShenheyuany(cmsArticle.getShenheyuany());
//		cmsArticleService.updateById(cmsArticle);
		 cmsArticleService.updateById(cmsArticle1);
//		cmsArticleContentService.saveOrUpdate(cmsArticleContent);

		 return Result.OK("审核成功!");
	 }
	 /**
	  *  审核回退
	  *
	  * @param cmsArticle
	  * @return
	  */
	 @AutoLog(value = "网站新闻表-审核回退")
	 @Operation(summary="网站新闻表-审核回退")
	 @RequiresPermissions("cms.article:cms_article:shenhe")
	 @RequestMapping(value = "/qx_shenhe", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<String> qx_shenhe(@RequestBody CmsArticle cmsArticle, HttpServletRequest request) {


		 String  username = JwtUtil.getUserNameByToken(request);
		 CmsArticle cmsArticle1 = new CmsArticle();
		 cmsArticle1.setId(cmsArticle.getId());
		 cmsArticle1.setZhuangt("4");
		 cmsArticle1.setShenhshj(new Date());
		 cmsArticle1.setShenheyuany(cmsArticle.getShenheyuany());
		 cmsArticle1.setShenhr(username);
//		cmsArticleService.updateById(cmsArticle);
		 cmsArticleService.updateById(cmsArticle1);
//		cmsArticleContentService.saveOrUpdate(cmsArticleContent);

		 return Result.OK("审核成功!");
	 }


	 /**
	  *  发布
	  *
	  * @param cmsArticle
	  * @return
	  */
	 @AutoLog(value = "网站新闻表-发布")
	 @Operation(summary="网站新闻表-发布")
	 @RequiresPermissions("cms.article:cms_article:publish")
	 @RequestMapping(value = "/publish", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<String> publish(@RequestBody CmsArticle cmsArticle, HttpServletRequest request) {

		 String  username = JwtUtil.getUserNameByToken(request);

		 CmsArticle cmsArticle1 = new CmsArticle();
		 cmsArticle1.setId(cmsArticle.getId());
		 cmsArticle1.setZhuangt("5");
		 cmsArticle1.setPublishdate(new Date());
//		cmsArticleService.updateById(cmsArticle);
		 cmsArticleService.updateById(cmsArticle1);
//		cmsArticleContentService.saveOrUpdate(cmsArticleContent);

		 return Result.OK("发布成功!");
	 }
	 /**
	  *  发布回退
	  *
	  * @param cmsArticle
	  * @return
	  */
	 @AutoLog(value = "网站新闻表-发布回退")
	 @Operation(summary="网站新闻表-发布回退")
	 @RequiresPermissions("cms.article:cms_article:publish")
	 @RequestMapping(value = "/qx_publish", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<String> qx_publish(@RequestBody CmsArticle cmsArticle, HttpServletRequest request) {


		 String  username = JwtUtil.getUserNameByToken(request);
		 CmsArticle cmsArticle1 = new CmsArticle();
		 cmsArticle1.setId(cmsArticle.getId());
		 cmsArticle1.setZhuangt("2");
//		 cmsArticle1.setShenhshj(new Date());
//		 cmsArticle1.setShenheyuany(cmsArticle.getShenheyuany());
//		 cmsArticle1.setShenhr(username);
//		cmsArticleService.updateById(cmsArticle);
		 cmsArticleService.updateById(cmsArticle1);
//		cmsArticleContentService.saveOrUpdate(cmsArticleContent);

		 return Result.OK("取消发布成功!");
	 }


	 /**
	  *  授权
	  *
	  * @param cmsArticle
	  * @return
	  */
	 @AutoLog(value = "网站新闻表-授权")
	 @Operation(summary="网站新闻表-审核回退")
	 @RequiresPermissions("cms.article:cms_article:shenhe")
	 @RequestMapping(value = "/save_jsqx_xw", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<String> save_jsqx_xw(@RequestBody CmsArticle cmsArticle, HttpServletRequest request) {


		 String  username = JwtUtil.getUserNameByToken(request);
		 CmsArticle cmsArticle1 = new CmsArticle();
		 cmsArticle1.setId(cmsArticle.getId());
		 cmsArticle1.setZhuangt("4");
		 cmsArticle1.setShenhshj(new Date());
		 cmsArticle1.setShenheyuany(cmsArticle.getShenheyuany());
		 cmsArticle1.setShenhr(username);
//		cmsArticleService.updateById(cmsArticle);
		 cmsArticleService.updateById(cmsArticle1);
//		cmsArticleContentService.saveOrUpdate(cmsArticleContent);

		 return Result.OK("审核成功!");
	 }

}
